package com.learn.demo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 创建交换机和消息队列配置
 *
 * @author banjiawei
 * @date 2021/11/22
 */
@Configuration
public class RabbitMQConfig {
    /**exchange名称*/
    public static final String STUDY_EXCHANGE = "studyExchange";
    /**延迟队列exchange名称*/
    public static final String LAZY_EXCHANGE = "Ex.LazyExchange";
    /**延迟队列queue名称*/
    public static final String LAZY_QUEUE = "MQ.LazyQueue";
    /**延迟key名称*/
    public static final String LAZY_KEY = "lazy.#";
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(STUDY_EXCHANGE, true, false);
    }

    @Bean
    public TopicExchange lazyExchange(){
        //延迟设置
        TopicExchange exchange = new TopicExchange(LAZY_EXCHANGE, true, false);
        exchange.setDelayed(true);
        return exchange;
    }

    @Bean
    public Queue lazyQueue(){
        return new Queue(LAZY_QUEUE, true);
    }

    @Bean
    public Binding lazyBinding(){
        return BindingBuilder.bind(lazyQueue()).to(lazyExchange()).with(LAZY_KEY);
    }
}
